package cn.youwan.market;

import java.util.ArrayList;

import cn.youwan.market.Fragment.CollectionFragment;
import cn.youwan.market.Fragment.FriendNewsFragment;
import cn.youwan.market.Fragment.HistoryFragment;
import cn.youwan.market.Fragment.RemindMessageFragment;
import cn.youwan.market.Fragment.TeamMeetingsFragment;
import cn.youwan.market.adapter.FragmentPageAdapter;
import cn.youwan.market.adapter.MypagerAdapter;
import cn.youwan.market.utils.Utils;
import android.os.Bundle;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PersonalActivity extends FragmentActivity implements OnClickListener{

	private LocalActivityManager manager;
	private ViewPager homeViewPager;
	private ImageView jiantou;
	private ArrayList<View> homeListViews;
	private TextView[] texts;
	private ArrayList<Fragment> framelist;
    public static PersonalActivity instance;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personal);
		instance=this;
		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);
		initHomeViewPager();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.personal, menu);
		return true;
	}

	public void setCurrentPage(int currentPage){
		homeViewPager.setCurrentItem(currentPage);
		showSelectBar(currentPage);
	}



	private void initHomeViewPager() {
		jiantou = (ImageView) findViewById(R.id.user_line_img);
		homeViewPager = (ViewPager) findViewById(R.id.user_viewPager);
		//		homeListViews = new ArrayList<View>();
		//		Intent intent1 = new Intent(this, RefreshActivity.class);
		//		homeListViews.add(getView("RefreshActivity", intent1));
		//		Intent intent2 = new Intent(this, DownloadActivity.class);
		//		homeListViews.add(getView("DownloadActivity", intent2));
		//		Intent intent3 = new Intent(this, DownloadingActivity.class);
		//		homeListViews.add(getView("DownloadingActivity", intent3));
		//		Intent intent4 = new Intent(this, UninstallActivity.class);
		//		homeListViews.add(getView("UninstallActivity", intent4));
		//		Intent intent5 = new Intent(this, UninstallActivity.class);
		//		homeListViews.add(getView("UninstallActivity", intent5));

		framelist=new ArrayList<Fragment>();
		framelist.add(new FriendNewsFragment());
		framelist.add(new RemindMessageFragment());
		framelist.add(new CollectionFragment());
		framelist.add(new HistoryFragment());
		framelist.add(new TeamMeetingsFragment());




		homeViewPager.setAdapter(new FragmentPageAdapter(getSupportFragmentManager(), framelist));
		homeViewPager.setOnPageChangeListener(new HomeViewPageChangeListener());
		texts = new TextView[5];
		texts[0] = (TextView) findViewById(R.id.user_text1);
		texts[1] = (TextView) findViewById(R.id.user_text2);
		texts[2] = (TextView) findViewById(R.id.user_text3);
		texts[3] = (TextView) findViewById(R.id.user_text4);
		texts[4] = (TextView) findViewById(R.id.user_text5);
		for (TextView t : texts) {
			t.setOnClickListener(this);
		}
		showSelectBar(0);

	}
	// 滑动页面更改事件监听器
	private class HomeViewPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int index) {
			showSelectBar(index);
		}
	}

	private View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}


	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.user_text1) {
			homeViewPager.setCurrentItem(0);
		} else if (v.getId() == R.id.user_text2) {
			homeViewPager.setCurrentItem(1);
		} else if (v.getId() == R.id.user_text3) {
			homeViewPager.setCurrentItem(2);
		} else if (v.getId() == R.id.user_text4) {
			homeViewPager.setCurrentItem(3);
		}else if (v.getId() == R.id.user_text5) {
			homeViewPager.setCurrentItem(4);
		}
	}



	private void showSelectBar(int index) {
		for (int i = 0; i < texts.length; i++) {
			if (index == i) {
				texts[i].setTextColor(this.getResources().getColor(
						R.color.left_menu));
			} else {
				texts[i].setTextColor(this.getResources().getColor(
						R.color.black_bg));
			}
		}
		int pos = (int) (Utils.getPhoneWidth(this) / 5);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pos,
				(int) (Utils.getDensity(this) * 5));
		int left = pos * index;
		lp.setMargins(left, (int) (Utils.getDensity(this) * 8), 0, 0);
		jiantou.setLayoutParams(lp);
	}
}
